package com.qingmeng.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.qingmeng.mapper.*;
import com.qingmeng.model.*;
import com.qingmeng.portal.model.dto.BlogPublishParam;
import com.qingmeng.portal.model.vo.BlogContentVo;
import com.qingmeng.portal.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private PhotosMapper photosMapper;
    @Autowired
    private BlogCommentMapper blogCommentMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 发布博客
     *
     * @param param 前端传过来的博客信息
     * @return 发布成功放回1 发布失败返回0
     */
    @Transactional//事务提交
    @Override
    public int publishBlog(BlogPublishParam param, Long publisherId) {
        int result = 0;
        //判断用户是否存在
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(publisherId);
        long l = userMapper.countByExample(userExample);
        if (l == 0) {
            return result;
        }
        //先获取blog表中的数据
        Blog blog = new Blog();
        blog.setBlogTitle(param.getBlogTitle());
        blog.setBlogContext(param.getBlogContext());
        blog.setPublishTime(new Date());
        blog.setBlogPublisherId(publisherId);
        blog.setBlogInfo(param.getBlogInfo());
        //往blog表中插入数据
        result = blogMapper.insertSelective(blog);
        //获取前端传过来的图片列表
        List<Photos> photosList = param.getPhotosList();
        if (photosList.size() > 0 && photosList != null) {
            for (Photos photo : photosList) {
                if (photo.getPhotoUrl() != null && !"string".equals(photo.getPhotoUrl())) {
                    Photos photos = new Photos();
                    photos.setBlogId(blog.getId());
                    photos.setPhotoName(photo.getPhotoName());
                    photos.setPhotoUrl(photo.getPhotoUrl());
                    photos.setCreateTime(new Date());
                    photos.setPhotoTypeId(1L);
                    photos.setUserId(publisherId);
                    if (photosMapper.insertSelective(photos) <= 0) {
                        return 0;
                    }
                }
            }
        }
        return result;
    }


    /**
     * 通过博客id查询博客内容
     *
     * @param id 博客id
     * @return 博客内容
     */
    @Override
    public BlogContentVo getBlogById(Long id) {
        BlogContentVo blogContentVo = new BlogContentVo();
        Blog blog = blogMapper.selectByPrimaryKey(id);
        //判断该blog是否存在
        if (blog == null) {
            return null;
        }

        //判断该blog是否被删除 0表示没被删除
        if (blog.getDeleteTag() == 0) {
            //获取blog表中的数据
            blogContentVo.setBlogId(blog.getId());
            blogContentVo.setBlogContext(blog.getBlogContext());
            blogContentVo.setBlogTitle(blog.getBlogTitle());
            blogContentVo.setBlogPublisherId(blog.getBlogPublisherId());
            blogContentVo.setPublishTime(blog.getPublishTime());
            blogContentVo.setBlogInfo(blog.getBlogInfo());
            //获取博客作者
            User user = userMapper.selectByPrimaryKey(blog.getBlogPublisherId());
            blogContentVo.setBlogPublisherName(user.getUsername());
            //获取博客相关图片表
            PhotosExample photosExample = new PhotosExample();
            photosExample.createCriteria().andBlogIdEqualTo(id);
            List<Photos> photos = photosMapper.selectByExample(photosExample);
            blogContentVo.setPhotosList(photos);
            //获取博客相关评论内容
            BlogCommentExample blogCommentExample = new BlogCommentExample();
            blogCommentExample.createCriteria().andBlogIdEqualTo(id);
            List<BlogComment> blogComments = blogCommentMapper.selectByExample(blogCommentExample);
            List<Comment> commentList = new ArrayList<>();
            for (BlogComment blogComment : blogComments) {
                Comment comment = commentMapper.selectByPrimaryKey(blogComment.getCommentId());
                commentList.add(comment);
            }
            blogContentVo.setCommentList(commentList);
            return blogContentVo;
        }
        blogContentVo = null;
        return blogContentVo;
    }

    /**
     * 根据博客标题获取博客内容
     * @param title 博客标题
     * @param pageNum 页码
     * @param pageSize 每页几条数据
     * @return 符合要去的博客
     */
    @Override
    public List<BlogContentVo> getBlogByTitle(String title, int pageNum, int pageSize) {
        //设置分页参数 只会对下面第一个sql有效
        PageHelper.startPage(pageNum, pageSize);
        List<BlogContentVo> blogContentVoList = new ArrayList<>();
        BlogExample blogExample = new BlogExample();
        //查询没有被删除的blog 0表示没被删除
        blogExample.createCriteria().andBlogTitleEqualTo(title).andDeleteTagEqualTo(0);
        List<Blog> blogs = blogMapper.selectByExample(blogExample);
        for (Blog blog1 : blogs) {
            Blog blog = blogMapper.selectByPrimaryKey(blog1.getId());
                BlogContentVo blogContentVo = new BlogContentVo();
                //先获取博客表的内容
                blogContentVo.setBlogId(blog.getId());
                blogContentVo.setBlogContext(blog.getBlogContext());
                blogContentVo.setBlogTitle(blog.getBlogTitle());
                blogContentVo.setBlogPublisherId(blog.getBlogPublisherId());
                blogContentVo.setPublishTime(blog.getPublishTime());
                blogContentVo.setBlogInfo(blog.getBlogInfo());
                //获取博客作者
                User user = userMapper.selectByPrimaryKey(blog.getBlogPublisherId());
                blogContentVo.setBlogPublisherName(user.getUsername());
                //获取博客相关图片
                PhotosExample photosExample = new PhotosExample();
                photosExample.createCriteria().andBlogIdEqualTo(blog.getId());
                List<Photos> photos = photosMapper.selectByExample(photosExample);
                blogContentVo.setPhotosList(photos);
                //获取博客相关评论
                BlogCommentExample blogCommentExample = new BlogCommentExample();
                blogCommentExample.createCriteria().andBlogIdEqualTo(blog.getId());
                List<BlogComment> blogComments = blogCommentMapper.selectByExample(blogCommentExample);
                List<Comment> commentList = new ArrayList<>();
                for (BlogComment blogComment : blogComments) {
                    Comment comment = commentMapper.selectByPrimaryKey(blogComment.getCommentId());
                    commentList.add(comment);
                }
                blogContentVo.setCommentList(commentList);
                blogContentVoList.add(blogContentVo);

        }
        return blogContentVoList;
    }

    /**
     * 获取所有博客
     * @param pageNum  页码
     * @param pageSize 每页几条数据
     * @return 所有博客
     */
    @Override
    public List<BlogContentVo> getAllBlogs(int pageNum, int pageSize) {
        //设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        List<BlogContentVo> blogContentVoList = new ArrayList<>();
        //获取所有blog
        BlogExample blogExample = new BlogExample();
        //查询没有被删除的blog 0表示没被删除
        blogExample.createCriteria().andDeleteTagEqualTo(0);
        List<Blog> blogs = blogMapper.selectByExample(blogExample);
        for (Blog blog1 : blogs) {
            Blog blog = blogMapper.selectByPrimaryKey(blog1.getId());
                BlogContentVo blogContentVo = new BlogContentVo();
                //先获取博客表的内容
                blogContentVo.setBlogId(blog.getId());
                blogContentVo.setBlogTitle(blog.getBlogTitle());
                blogContentVo.setBlogContext(blog.getBlogContext());
                blogContentVo.setBlogPublisherId(blog.getBlogPublisherId());
                blogContentVo.setPublishTime(blog.getPublishTime());
                blogContentVo.setBlogInfo(blog.getBlogInfo());
                //获取博客作者
                User user = userMapper.selectByPrimaryKey(blog.getBlogPublisherId());
                blogContentVo.setBlogPublisherName(user.getUsername());
                //获取博客相关图片
                PhotosExample photosExample = new PhotosExample();
                photosExample.createCriteria().andBlogIdEqualTo(blog.getId());
                List<Photos> photos = photosMapper.selectByExample(photosExample);
                blogContentVo.setPhotosList(photos);
                //获取博客相关评论
                BlogCommentExample blogCommentExample = new BlogCommentExample();
                blogCommentExample.createCriteria().andBlogIdEqualTo(blog.getId());
                List<BlogComment> blogComments = blogCommentMapper.selectByExample(blogCommentExample);
                List<Comment> commentList = new ArrayList<>();
                for (BlogComment blogComment : blogComments) {
                    Comment comment = commentMapper.selectByPrimaryKey(blogComment.getCommentId());
                    commentList.add(comment);
                }
                blogContentVo.setCommentList(commentList);
                blogContentVoList.add(blogContentVo);

        }
        return blogContentVoList;
    }

    /**
     * 根据博客id修改博客内容
     *
     * @param id    博客id
     * @param param 需要修改的内容
     * @return 修改过后的内容
     */
    @Override
    public BlogPublishParam alterBlogById(Long id, BlogPublishParam param) {
        BlogPublishParam blogPublishParam = new BlogPublishParam();
        //获取blog表中原来的内容 并判断是否更改
        Blog blog = blogMapper.selectByPrimaryKey(id);
        //判断该blog是否被删除 0表示没被删除
        if (blog == null || blog.getDeleteTag() == 1) {
            return null;
        }
        //将修改的参数传递给blog
        if (param.getBlogTitle() != null) {
            blog.setBlogTitle(param.getBlogTitle());
        }
        if (param.getBlogContext() != null) {
            blog.setBlogContext(param.getBlogContext());
        }
        if (param.getBlogInfo() != null) {
            blog.setBlogInfo(param.getBlogInfo());
        }
        //添加blog的修改时间
        blog.setUpdateTime(new Date());
        //将更改后的blog表在数据库中修改
        BlogExample blogExample = new BlogExample();
        blogExample.createCriteria().andIdEqualTo(id);
        blogMapper.updateByExampleSelective(blog, blogExample);
        //将更改后的blog表中的数据赋值给blogPublishParam
        blogPublishParam.setBlogId(blog.getId());
        blogPublishParam.setBlogTitle(blog.getBlogTitle());
        blogPublishParam.setBlogContext(blog.getBlogContext());
        blogPublishParam.setBlogInfo(blog.getBlogInfo());

        /*PhotosExample photosExample = new PhotosExample();
        photosExample.createCriteria().andBlogIdEqualTo(id);
        List<Photos> photos = photosMapper.selectByExample(photosExample);
        if (photos !=null && photos.size() > 0) {
            blogPublishParam.setPhotosList(photos);
        }*/

        //判断是否修改图片
        if (param.getPhotosList().size() > 0 && param.getPhotosList() != null) {
            List<Photos> photosList1 = param.getPhotosList();
            List<Photos> photosList = new ArrayList<>();
            for (Photos photos1 : photosList1) {
                if (photos1.getPhotoUrl() != null && !"string".equals(photos1.getPhotoUrl())){
                    photos1.setUserId(blog.getBlogPublisherId());
                    photos1.setBlogId(blog.getId());
                    photos1.setPhotoTypeId(1L);
                    photos1.setCreateTime(new Date());
                    //符合要求的photos1对象添加到photosList中
                    photosList.add(photos1);
                    //将修改后的图片在数据库中做修改 先删除原来的图片
                    PhotosExample photosExample = new PhotosExample();
                    photosExample.createCriteria().andBlogIdEqualTo(id);
                    photosMapper.deleteByExample(photosExample);
                    //插入新的图片
                    photosMapper.insertSelective(photos1);
                }
            }
            blogPublishParam.setPhotosList(photosList);
        }
        //如果没有要修改的图片就将原来的图片传给blogPublishParam
        if (blogPublishParam.getPhotosList().size() == 0 || blogPublishParam == null) {
            PhotosExample photosExample = new PhotosExample();
            photosExample.createCriteria().andBlogIdEqualTo(id);
            List<Photos> photos = photosMapper.selectByExample(photosExample);
            if (photos !=null && photos.size() > 0) {
                blogPublishParam.setPhotosList(photos);
            }
        }

        return blogPublishParam;


    }

    /**
     * 根据博客id删除博客
     *
     * @param id 博客id
     * @return 1为删除成功 0为删除失败
     */
    @Override
    public int deleteBlogById(Long id) {
        //获取被删除的blog信息
        Blog blog = blogMapper.selectByPrimaryKey(id);
        if (blog == null){
            return 0;
        }
        //判断该blog是否被删除 0表示没被删除
        if (blog.getDeleteTag() == 0) {
            blog.setDeleteTag(1);
            BlogExample blogExample = new BlogExample();
            blogExample.createCriteria().andIdEqualTo(id);
            blogMapper.updateByExampleSelective(blog, blogExample);
            return 1;
        }
        return 0;
    }

    /**
     * 评论博客
     *
     * @param id      要评论的博客id
     * @param comment 评论内容
     * @param tag     是否点赞该博客 0 表示不点赞 1表示点赞
     * @return 1表示评论成功 0表示评论失败
     */
    @Override
    public int commentBlog(Long id, String comment, int tag, Long userId) {
        int result = 0;
        //判断用户是否存在
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(userId);
        long l = userMapper.countByExample(userExample);
        if (l == 0) {
            return result;
        }
        //获取要评论的blog
        Blog blog = blogMapper.selectByPrimaryKey(id);
        //判断博客是否存在或被删除 0是没被删除
        if (blog != null && blog.getDeleteTag() == 0) {
            //判断是否点赞该博客
            if (tag == 1) {
                int i = blog.getBlogLikeNumber() + 1;
                blog.setBlogLikeNumber(i);
                BlogExample blogExample = new BlogExample();
                blogExample.createCriteria().andIdEqualTo(id);
                blogMapper.updateByExampleSelective(blog, blogExample);
            }
            //将评论存储进数据库
            Comment comment1 = new Comment();
            comment1.setComment(comment);
            comment1.setLikeTag(tag);
            comment1.setCreateTime(new Date());
            comment1.setUserId(userId);
            commentMapper.insert(comment1);
            //将博客id和对应评论id插入blog_comment表中
            BlogComment blogComment = new BlogComment();
            blogComment.setBlogId(id);
            blogComment.setCommentId(comment1.getId());
            blogComment.setCreateTime(new Date());
            result = blogCommentMapper.insertSelective(blogComment);
            return result;
        }
        return 0;
    }
}
